﻿#region Header Licence
//  NKnobMan, Copyright 2009, Alexandre MUTEL.
// 
//  This is free software; you can redistribute it and/or modify it
//  under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of
//  the License, or (at your option) any later version.
// 
//  This software is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
// 
//  You should have received a copy of the GNU Lesser General Public
//  License along with this software; if not, write to the Free
//  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
//  02110-1301 USA, or see the FSF site: http://www.fsf.org.
#endregion
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace NKnobMan
{
    public partial class KnobManProcessSelectorForm : Form
    {
        public KnobManProcessSelectorForm()
        {
            InitializeComponent();
            PopulateKnobManProcesses();
        }

        private void PopulateKnobManProcesses()
        {
            listBoxKnobManProcesses.Items.Clear();
            // List KnobMan Processes
            Process[] processes = Process.GetProcessesByName("KnobMan");
            listBoxKnobManProcesses.Items.AddRange(processes);

            if (processes.Length == 0)
            {
                buttonOk.Enabled = false;
            }
            else
            {
                listBoxKnobManProcesses.SelectedItem = processes[0];
            }
        }

        public bool SingleKnobMan
        {
            get
            {
                return listBoxKnobManProcesses.Items.Count == 1;
            }
        }

        public IntPtr SelectedKnobManHandle
        {
            get
            {
                if ( listBoxKnobManProcesses.SelectedItem != null)
                {
                    return ((Process) listBoxKnobManProcesses.SelectedItem).MainWindowHandle;
                }
                return IntPtr.Zero;
            }
        }

        private void listBoxKnobManProcesses_Format(object sender, ListControlConvertEventArgs e)
        {
            Process process = (Process) e.ListItem;
            e.Value = process.ProcessName + " (" + process.Id + ")";
        }

        private void listBoxKnobManProcesses_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            buttonOk.Enabled = listBoxKnobManProcesses.SelectedItem != null;
        }

        private void KnobManProcessSelectorForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (e.CloseReason != CloseReason.None)
            {
                DialogResult = DialogResult.Cancel;
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            PopulateKnobManProcesses();
        }
    }
}
